package com.wiz.weixin.controllor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class RequestInterceptor extends HandlerInterceptorAdapter {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handle) {
		String requestURI = request.getRequestURI();
		requestURI = requestURI == null ? "" : requestURI;

		if (requestURI.indexOf("/tencent/") > 0) {//腾讯渠道，暂时只有微信
			return true;
		}
		try {
			// 出现了乱码问题
			request.setCharacterEncoding("UTF-8");
//			UserTracker userTracker = clientUtil
//					.getUserTrackerFromRequest(request);
//			if (userTracker == null) {
//				response.sendRedirect("http://api.digua.d.cn/403.jsp");
//				return false;
//			}
//			// 判断用户是否合法，合法的用户放入Request
//			if (userTracker.isValid()) {
//				// 统计日志
//				DiguaUserRecorder.record(userTracker, requestURI);
//				request.setAttribute(Constant.REQ_ATTR_KEY_USERTRACKER,
//						userTracker);
//				return true;
//			}
//			response.sendRedirect("http://api.digua.d.cn/403.jsp");
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("拦截器异常：" + e, e);
			try {
				response.sendRedirect("http://api.digua.d.cn/500.jsp");
			} catch (IOException e1) {
				e1.printStackTrace();
				return false;
			}
			return false;
		}
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}
